package com.masz.transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author Ma.sz 2023/2/13 17:57
 */
@Service
public class TransactionalTestService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void a() {
        jdbcTemplate.execute("insert into abc_test(a) values (1)");
        try {
            c();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1/0;
    }


    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public void c(){
        jdbcTemplate.execute("insert into abc_test2(a) values (1)");
        int i = 1/0;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public void d(){
        jdbcTemplate.execute("insert into abc_test2(a) values (1)");
    }

}
